home *** CD-ROM | disk | FTP | other *** search
/ MacGames Sampler / PHT MacGames Bundle.iso / MacSource Folder / Samples from the CD / C and C++ / POSIX / ThinkCPosix / dup2.c < prev    next >
Text File  |  1992-09-11  |  595b  |  26 lines

  1. /* $Id: $ */
  2.  
  3. /*
  4.  * This implementation of dup2() disables oldfd;`
  5.  * in effect, it assumes that oldfd is closed
  6.  * immediately after dup2() (as is almost always the case).
  7.  * The problem is that if __file[oldfd]->func() is left unchanged
  8.  * then close(oldfd) will also close newfd.
  9.  */
  10.  
  11. #include "ThinkCPosix.h"
  12.  
  13. int dup2(int oldfd, int newfd)
  14. {
  15.     FILE *oldfp = fdopen(oldfd, "");
  16.  
  17.     if (oldfp == NULL || (unsigned)newfd >= FOPEN_MAX) {
  18.         errno = EBADF;
  19.         return -1;
  20.     }
  21.     close(newfd);
  22.     memcpy(&__file[newfd], (char*)oldfp, sizeof(FILE));
  23.     memset((char*)oldfp, 0, sizeof(FILE));
  24.     return newfd;
  25. }
  26.